
# 解法一：位运算
# class Solution:
#     def reverseBits(self, n: int) -> int:
#         res = 0
#         for _ in range(32):
#             res <<= 1
#             res += n & 1
#             n >>= 1
#
#         return res


# 解法二: 时间较长
# class Solution:
#     def reverseBits(self, n: int) -> int:
#         res_list = []
#         if n==0:
#             return 0
#         if n == 1:
#             return 1<<31
#
#         while n >= 2:
#             n, yu_shu = divmod(n, 2)
#             print("shang: %d, yu_shu: %d" % (n, yu_shu))
#             res_list.append(yu_shu)
#         res_list.append(n)
#         # 000000 1010 0101 000001111 0100 11100
#         print("res_list: ", res_list)
#         length = len(res_list)
#         result = 0
#         for i in range(length):
#             result += res_list[i] * (2 ** (32 - 1 - i))
#             print(result)
#
#         return result


# n & 1 与n & 0x1的区别是什么？为什么n & 0x1的效率更高？

class Solution:
    def reverseBits(self, n: int) -> int:
        ret = 0
        for i in range(32):
            m = n & 1
            n = n >> 1
            ret = ret + (m << (31 - i))
        return ret


if __name__ == "__main__":
    s = Solution()
    res = s.reverseBits(43261596)
    print(res)
